<template>
  <div class="password">
    <div class="title">
      <p>验证码登录</p>
    </div>
    <div class="form">
      <input
        type="text"
        placeholder="请输入手机号"
        class="phone"
        v-model="phone"
      />
      <div class="password-input">
        <input
          type="password"
          placeholder="请输入短信验证码"
          ref="input"
          v-model="code"
        />
        <span @click="stopToClick" v-if="showCountDown" class="count-down"
          >{{ count }}秒后重新获取</span
        >
        <span v-else @click="getCode">获取验证码</span>
      </div>

      <div class="submit" @click="toLogin">登录</div>
      <div class="choose">
        <span @click="$router.push('/login')">密码登录</span>
        <span @click="$router.push('/login/register')">用户注册</span>
      </div>
      <div class="codition">
        <span>登录即代表您已同意</span>
        <span>《用户隐私政策》</span>
      </div>
    </div>
  </div>
</template>

<script>
import qs from "qs";

import Vue from "vue";
import { Toast } from "vant";
Vue.use(Toast);
export default {
  methods: {
    async getCode() {
      let res = await this.axios(
        `https://m-mall.gxptkc.com/v3/msg/front/commons/smsCode?mobile=${this.phone}&type=free`
      );

      Toast.success(res.data.msg);
      if (res.data.msg == "发送成功") {
        this.showCountDown = true;
        this.time = setInterval(() => {
          this.count -= 1;
        }, 1000);
      }
    },
    stopToClick() {
      Toast.fail("请勿重复获取！");
    },
    async toLogin() {
      let res = await this.axios({
        method: "POST",
        url: "https://m-mall.gxptkc.com/v3/member/front/login/freeSecretLogin",
        data: qs.stringify({
          mobile: this.phone,
          smsCode: this.code,
        }),
      });
      if (res.data.msg == "成功") {
        this.token = res.data.data.access_token;
        Toast.success("登录成功");
        setTimeout(() => {
          this.$router.push("/own");
          localStorage.setItem("fastCar-token", this.token);
        }, 1000);
      } else {
        Toast.fail(res.data.msg);
      }
    },
  },
  data() {
    return {
      phone: 13679571763,
      code: null,
      token: null,
      showCountDown: false,
      count: 60,
      time: null,
    };
  },
  watch: {
    token(newVal) {
      localStorage.setItem("fastCar-token", newVal);
    },
    count(c) {
      if (c <= 1) {
        this.count = 60;
        this.showCountDown = false;
        clearInterval(this.time);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
$color: #eee;
.password {
  box-sizing: border-box;
  padding: 0 10rem;
  display: flex;
  flex-direction: column;
  .title {
    p {
      font-size: 18rem;
      color: #333;
      font-weight: bold;
    }
  }
  .form {
    flex-grow: 1;
    margin-top: 30rem;
    position: relative;
    input {
      border: none;
      font-size: 15rem;
    }
    input.phone {
      width: 100%;
      border-bottom: 1px solid $color;
      padding: 10rem 0;
      //   color: #ccc;
      margin-bottom: 30rem;
    }
    .password-input {
      border-bottom: 1px solid $color;
      padding: 10rem 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 40rem;
      input {
        width: 60%;
      }

      span {
        color: #fc1c1c;
        padding: 4rem;
        border: 1rem solid #fc1c1c;
        &.count-down {
          border: 1rem solid #ccc;
          color: #ccc;
          cursor: not-allowed;
        }
      }
    }
    .submit {
      width: 100%;
      height: 44rem;
      border: none;
      background: linear-gradient(90deg, #fc1f1d, #fd7326);
      border-radius: 22rem;

      color: #fff;
      font-size: 18rem;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .choose {
      display: flex;
      justify-content: center;
      margin-top: 15rem;
      span {
        font-size: 14rem;
        color: #2d2d2d;
        padding: 0 10rem;
        &:last-child {
          color: #fc1c1c;
          border-left: 1px solid $color;
        }
      }
    }
    .codition {
      width: 100%;
      position: absolute;
      bottom: 30rem;
      display: flex;
      justify-content: center;
      span {
        font-size: 13rem;
        color: #999;
        &:last-child {
          color: #fc1e1e;
        }
      }
    }
  }
}
</style>
